
#include "config.h"
#include "MainTask.h"
#include "TM1618.h"
#include "delay.h"

void stb(unsigned char value)
{

	if (value)
		P12 = 1;
	else
		P12 = 0;
}
void dio(unsigned char value)
{

	//GPIO_Init(DOOR_OUT_PORT,DOOR_OUT_PIN,GPIO_MODE_OUT_PP_HIGH_SLOW);
	if (value)
		P14 = 1;
	else
		P14 = 0;
}

void clk(unsigned char value)
{

	//GPIO_Init(DOOR_OUT_PORT,DOOR_OUT_PIN,GPIO_MODE_OUT_PP_HIGH_SLOW);
	if (value)
		P03 = 1;
	else
		P03 = 0;
}

//---------------------TM1618����1�ֽ������ӳ���------------------------------------------

void TM1618write(unsigned char wr_date)
{
	unsigned char i;

	stb(0);
	Delay(1);

	for (i = 0; i < 8; i++)
	{
		clk(0);
		Delay(1);

		if ((wr_date & 0x01) != 0)
		{
			dio(1);
		}
		else
		{
			dio(0);
		}

		Delay(1);
		clk(1);
		Delay(1);
		wr_date = wr_date >> 1;
	}
}

//-------------------------TM1618���ù̶���ַ��ʾ�����ӳ���--------------------------------------------

void TM1618show(unsigned char show_LED[])
{

	unsigned char i;
	stb(1);
	clk(1);
	dio(1);
	TM1618write(0x01); //��ʾģʽ����5λ7��
	stb(1);			   //��ʾģʽ������ϣ�STB��1
	TM1618write(0x40); //�����������ã�������ͨģʽ���̶���ַ��д���ݵ���ʾ�Ĵ���
	stb(1);			   //��������������ϣ�STB��1
	TM1618write(0xC0);

	for (i = 0; i < 5; i++)
	{
		TM1618write(show_LED[i]);
		TM1618write(show_LED[i] >> 2);
	}

	stb(1);
	TM1618write(0x88 + (LiangDu & 0x07)); //�����������ã�������ʾ������ʾ����
	stb(1);								  //��ʾ���ݷ������STB��1
}

/**
 * @brief  ��ʼ������LED��IO
 * @param  ��
 * @retval ��
 */
void ShuMaGuan_GPIO_Init(void)
{

	// GPIO_Init(TM1618_STB_PORT, TM1618_STB_PIN, GPIO_MODE_OUT_PP_HIGH_SLOW);
	// GPIO_Init(TM1618_CLK_PORT, TM1618_CLK_PIN, GPIO_MODE_OUT_PP_HIGH_SLOW);
	// GPIO_Init(TM1618_DIO_PORT, TM1618_DIO_PIN, GPIO_MODE_OUT_PP_HIGH_SLOW);
}

unsigned char show_LED[7] =
{
	0x3F, 0x06, 0x5B, 0x4F, 0x66
};
unsigned char number[16] =
{
	0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F,
	0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71
};
#if 0
unsigned char LED_hex[][2] =
{
	0x1f, 0x08, 0x06, 0x00, 0x1b, 0x10, 0x0f, 0x10, //0 1 2 3
	0x06, 0x18, 0x0d, 0x18, 0x1d, 0x18, 0x07, 0x00, //4 5 6 7
	0x1f, 0x18, 0x0f, 0x18, 0x17, 0x18, 0x1c, 0x18, //8 9 A b
	0x19, 0x08, 0x1e, 0x10, 0x19, 0x18, 0x11, 0x18 //C d E F
};
#endif
/*
 * ��������Task_LED1
 * ����  : LED����1��
 *		     ���ȼ�Ϊ4��
 *		     ��100ms�ļ���������ر�LED1
 * ����  ����
 * ���  : ��
 */

#define ZUO 1
#define YOU 2

static short WorningCnt;
void Task_TM1618(void *p_arg)
{
	static unsigned char cnt;
	unsigned char FangXiang;
	unsigned char i;

	if (Worning & 0x08)
		FangXiang = ZUO;

	if (Worning & 0x40)
		FangXiang = YOU;

	if (FangXiang == ZUO)
	{

		for (i = 0; i < 5; i++)
		{
			show_LED[i] = 0x08;
		}

		show_LED[cnt] = number[8];

		if (cnt > 0)
			cnt--;
		else
			cnt = 4;
	}
	else if (FangXiang == YOU)
	{
		for (i = 0; i < 5; i++)
		{
			show_LED[i] = 0x08;
		}

		show_LED[cnt] = number[8];

		cnt++;
		cnt = cnt % 5;
	}
	else if (Twinkle == 0)
	{
		show_LED[0] = LED[4];
		show_LED[1] = LED[3];
		show_LED[2] = LED[2];
		show_LED[3] = LED[1];
		show_LED[4] = LED[0];
	}
	else
	{
		if (WorningCnt == 1)
		{
			WorningCnt = 0;
			show_LED[0] = LED[4];
			show_LED[1] = LED[3];
			show_LED[2] = LED[2];
			show_LED[3] = LED[1];
			show_LED[4] = LED[0];
		}
		else
		{
			WorningCnt = 1;
			show_LED[0] = 0;
			show_LED[1] = 0;
			show_LED[2] = 0;
			show_LED[3] = 0;
			show_LED[4] = 0;
		}
	}

	TM1618show(show_LED);
}
